from django.shortcuts import render,HttpResponseRedirect,HttpResponse
from log_reg.models import *
from order.models import *
from eat.models import *
from django.views import View
from django.http import JsonResponse
from django.shortcuts import render
from read_json import *
from django.http import JsonResponse
import re
from eat.views import order_T
# Create your views here.
# 注册
def reg(request):
    order_T()
    if request.method == "GET":
        return render(request, 'reg.html',{'user':False})
    else:
        # 需要编写代码的位置
        username=request.POST.get('username')
        password=request.POST.get('password')

        error={}
        if not re.search('^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$',username):
            error['error_username']='用户名必须是邮箱'
        elif User.objects.filter(username=username):
            error['error_username']='用户名已存在，请重新输入'
        elif password=='':
            error['error_password']='密码不能为空'
        else:
            User.objects.create(username=username,password=password)
            request.session['user'] = username

            return HttpResponseRedirect('/logreg/user_data/')

        return render(request, 'reg.html', error)

# 登录
def log(request):
    order_T()
    if request.method == "GET":
        return render(request, 'login.html',{'user':False})
    else:
        # 需要编写代码的位置
        username = request.POST.get('username')
        password = request.POST.get('password')

        error = {}
        if username == '':
            error['error_username'] = '用户名不能为空'
        elif password == '':
            error['error_password'] = '密码不能为空'
            error['username']=username
        elif User.objects.filter(username=username,password=password):
            request.session['user']=username

            return HttpResponseRedirect('/logreg/user_data/')
        else:
            error['error_all']='用户名或密码错误，请重新输入'

        return render(request, 'login.html', error)

def user(request):
    return {'myuser':request.session.get('user')}

# 退出登录
def out_log(request):
    if request.session.get('user'):
    # 需要编写代码的位置
        request.session.flush()
    return HttpResponseRedirect('/')


# 用户资料
def user_data(request):
    order_T()
    if request.method == "GET":
        if not request.session.get('user'):
            return HttpResponseRedirect('/logreg/log/')

        user = request.session.get('user')
        user = User.objects.get(username=user)

        pro = getProvince()
        city = getCity('110000')
        area = getArea('110100')

        return render(request, 'user_data.html', {"user": user, 'pro': pro, 'city': city, 'area': area})


    else:
        # 判断用户是否要保存新电话和地址:
        if request.POST.get('type_baocun'):
            tel = request.POST.get('tel')
            if tel == '':
                tel = '0'
            pro = request.POST.get('pro')
            city = request.POST.get('city')
            area = request.POST.get('area')
            address_all = request.POST.get('address_all')

            # 将地址进行拼接
            address = pro+city+area+address_all
            # 将新电话和地址存入当前用户的数据库中
            User.objects.filter(username=request.session.get('user')).update(tel=tel,address=address)
        else:
            # 三级联动
            type_abc = request.POST.get('type')

            if type_abc == 'pro123':
                pro = request.POST.get('pro')
                city = getCity(pro)

                # 改变省的同时默认显示第一个城市的下辖县
                area = getArea(city[0]['id'])
                return JsonResponse({'city': city,'area': area})

            elif type_abc == 'city123':
                city = request.POST.get('city')
                area = getArea(city)
                return JsonResponse({'area': area})

        return HttpResponseRedirect('/logreg/user_data/')

def user_del(request):
    user = request.session.get('user')
    user = User.objects.get(username=user)
    user.tel = "0"
    user.address = "0"
    user.save()
    pro = getProvince()
    city = getCity('110000')
    area = getArea('110100')
    return render(request, 'user_data.html', {"user": user, 'pro': pro, 'city': city, 'area': area})